package com.mlm.mlmapp.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.mlm.mlmapp.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

//import com.yinduoziben.yplan.activity.DepositActivity;

/**
 * Created by Wangyingji on 15/9/2.
 */
public class WebViewScriptInterface extends Activity{
    private Context mContext;
    private Activity activity;
    private WebView webView;
    private String proxyInfoId;
    // 首先在您的Activity中添加如下成员变量
    final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
    private SocializeListeners.SnsPostListener snsPostListener;

    public WebViewScriptInterface(Activity activity, Context context, WebView webView,String proxyInfoId) {
        this.mContext = context;
        this.webView = webView;
        this.activity = activity;
        this.proxyInfoId=proxyInfoId;
    }

    @JavascriptInterface
    public void SharePartner() {
//        Toast.makeText(mContext, "JsCallAndroid", Toast.LENGTH_SHORT).show();
        snsPostListener = new SocializeListeners.SnsPostListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onComplete(SHARE_MEDIA platform, int stCode,
                                   SocializeEntity entity) {
                if (stCode == 200) {
                    Toast.makeText(mContext, "分享成功", Toast.LENGTH_SHORT)
                            .show();

                } else {
                }
            }
        };
        mController.registerListener(snsPostListener);
        //默认分享列表中存在的平台如果需要删除，则调用下面的代码：
        mController.getConfig().removePlatform(SHARE_MEDIA.TENCENT);
        configPlatforms();
        setShareContent();

        mController.openShare(activity, false);

    }

    /**
     * 配置分享平台参数</br>
     */
    private void configPlatforms() {
        // 添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());

        // 添加微信、微信朋友圈平台
        addWXPlatform();
    }


    /**
     * @return
     * @功能描述 : 添加微信平台分享
     */
    private void addWXPlatform() {
        // 注意：在微信授权的时候，必须传递appSecret
        // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
        String appId = "wxe94f6ebf0daa4bda";
        String appSecret = "e2f4f10444b6399f18e151737a0b653e";
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(mContext, appId, appSecret);
        wxHandler.addToSocialSDK();

        // 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(mContext, appId, appSecret);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
    }

    /**
     * 根据不同的平台设置不同的分享内容</br>
     */
    private void setShareContent() {
        // 配置SSO
        mController.getConfig().setSsoHandler(new SinaSsoHandler());

        UMImage localImage = new UMImage(mContext, R.drawable.appicon);
//
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setShareContent("来自美啦美的合伙人邀请" + "http://share.mlmsalon.com/Home/Share/proxy/pid/" + proxyInfoId);
        weixinContent.setTitle("美啦美");
        weixinContent.setTargetUrl("http://share.mlmsalon.com/Home/Share/proxy/pid/" + proxyInfoId);
        weixinContent.setShareMedia(localImage);

        mController.setShareMedia(weixinContent);

        // 设置朋友圈分享的内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent("来自美啦美的合伙人邀请" + "http://share.mlmsalon.com/Home/Share/proxy/pid/" + proxyInfoId);
        circleMedia.setTitle("美啦美");
        circleMedia.setShareMedia(localImage);
        circleMedia.setTargetUrl( "http://share.mlmsalon.com/Home/Share/proxy/pid/" + proxyInfoId);
        mController.setShareMedia(circleMedia);
//
        SinaShareContent sinaContent = new SinaShareContent();
        sinaContent.setTitle("美啦美");
        sinaContent.setShareContent("来自美啦美的合伙人邀请" + "http://share.mlmsalon.com/Home/Share/proxy/pid/" + proxyInfoId);
        sinaContent.setShareMedia(localImage);
        sinaContent.setTargetUrl( "http://share.mlmsalon.com/Home/Share/proxy/pid/" + proxyInfoId);
        mController.setShareMedia(sinaContent);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

}
